package com.luxshare.aos.demo;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.luxshare.aos.R;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 内容提供者示例
 */
public class ContentProviderActivity extends Activity {
    private static final String DEBUG_TAG = "ContentProviderActivity";

    private static final int REQUEST_CODE_READ_EXTERNAL_STORAGE = 0x01;

    private TextView textView;
    private ListView listView;
    private ContentResolver resolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_provider);

        listView = (ListView) findViewById(R.id.person_list);
        textView = (TextView) findViewById(R.id.person_count);
        textView.setText("共0位联系人");

        resolver = getContentResolver();
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
        Log.i(DEBUG_TAG, String.valueOf(ActivityCompat.checkSelfPermission(this, Manifest.permission.VIBRATE)));
        Log.i(DEBUG_TAG, String.valueOf(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)));

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE_READ_EXTERNAL_STORAGE);
        } else {
            countContacts();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_READ_EXTERNAL_STORAGE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    countContacts();
                }
                break;
        }
    }

    private void countContacts() {
        try {
            Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            if (cursor != null) {
                Integer count = cursor.getCount();
                textView.setText(MessageFormat.format("共{0}位联系人", count));
                List<String> contacts = new ArrayList<>();
                while (cursor.moveToNext()) {
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    contacts.add(name);
                }
                listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, contacts));
                cursor.close();
            }
        } catch (Exception e) {
            Log.e(DEBUG_TAG, e.getMessage());
        }
    }
}
